function fxtypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "系统级";
    else if (value == 2)
        return "类目级";
    else if (value == 3)
        return "商品级";
    else return ""
}

function huiyuantypeFormatter(value, row, index) {
    if (value == undefined || value == "") return "";
    else if (value == 1)
        return "充值模式";
    else if (value == 2)
        return "会员商品";
    else if (value == 3)
        return "累计消费";
    else if (value == 4)
        return "线下收款";
    else
        return ""
}

function changeState(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/fenxiao/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {

        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#hidfxlevel').val(0)
                    initSaveDiv()
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/fenxiao/list'});
                } else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function (dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！', text: '', timer: 2000})
        }
    })
}

var editsw = [];

/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var index = button.data('index');
    var modal = $(this);
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log(data)

    modal.find('#id').val(data.id)
    // modal.find('#hytype').find("option[value='" + data['hytype'] + "']").attr("selected", true);
    modal.find('#fxtype').find("option[value='" + data['fxtype'] + "']").attr("selected", true);
    modal.find('#fxlevel').find("option[value='" + data['fxlevel'] + "']").attr("selected", true);
    modal.find('#hylevel').find("option[value='" + data['hylevel'] + "']").attr("selected", true);
    modal.find('#huiyuantype').find("option[value='" + data['huiyuantype'] + "']").attr("selected", true);
    modal.find('#yejizhouqi').find("option[value='" + data['yejizhouqi'] + "']").attr("selected", true);
    mhylevelChange(data.hylevel)

    modal.find('#jiandian').val(data.jiandian)
    modal.find('#tuanjiang').val(data.tuanjiang)
    modal.find('#fxname1').val(data.fxname1)
    modal.find('#huifei1').val(data.huifei1)
    modal.find('#mzhekou1').val(data.mzhekou1)
    modal.find('#txfei1').val(data.txfei1)
    modal.find('#fxname2').val(data.fxname2)
    modal.find('#huifei2').val(data.huifei2)
    modal.find('#mzhekou2').val(data.mzhekou2)
    modal.find('#txfei2').val(data.txfei2)
    modal.find('#fxname3').val(data.fxname3)
    modal.find('#huifei3').val(data.huifei3)
    modal.find('#mzhekou3').val(data.mzhekou3)
    modal.find('#txfei3').val(data.txfei3)
    modal.find('#fxname4').val(data.fxname4)
    modal.find('#huifei4').val(data.huifei4)
    modal.find('#mzhekou4').val(data.mzhekou4)
    modal.find('#txfei4').val(data.txfei4)
    modal.find('#fxname5').val(data.fxname5)
    modal.find('#huifei5').val(data.huifei5)
    modal.find('#mzhekou5').val(data.mzhekou5)
    modal.find('#txfei5').val(data.txfei5)
    modal.find('#fxname6').val(data.fxname6)
    modal.find('#huifei6').val(data.huifei6)
    modal.find('#mzhekou6').val(data.mzhekou6)
    modal.find('#txfei6').val(data.txfei6)
    modal.find('#fxname7').val(data.fxname7)
    modal.find('#huifei7').val(data.huifei7)
    modal.find('#mzhekou7').val(data.mzhekou7)
    modal.find('#txfei7').val(data.txfei7)
    modal.find('#hytype').val(data.hytype)
    modal.find('#mpid').val(data.mpid)

})

//tableHeight函数
function tableHeight() {
    //可以根据自己页面情况进行调整
    return $(window).height() - 200;
}

function ahylevelChange() {
    //可以根据自己页面情况进行调整
    var level = $("#hylevel2 option:selected").val();
    console.log(level)
    for (var i = 1; i <= 7; i++) {
        if (i <= level) $("#arow" + i).show()
        else $("#arow" + i).hide()
    }
}

function mhylevelChange(level) {
    //可以根据自己页面情况进行调整
    var level = level || $("#hylevel option:selected").val();
    console.log(level)
    for (var i = 1; i <= 7; i++) {
        if (i <= level) $("#mrow" + i).show()
        else $("marow" + i).hide()
    }
}

function initSaveDiv() {
    if ($("#hidfxcout").val() >= 5) {
        $("#saveDiv").hide()
        // $("#tablediv").show()
    } else {
        $("#saveDiv").show()
        // $("#tablediv").hide()
    }
}

$(function () {

    var sw = [];
    $(".js-switch").each(function (i, e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })
    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })
    console.log("======init to hide arow")
    for (var i = 3; i <= 7; i++) {
        console.log("======init to hide arow i=" + i)
        $("#arow" + i).hide()
        $("#mrow" + i).hide()
    }
    initSaveDiv()
    var act = {
        title: '操作',
        field: 'Attribute',
        align: 'center',
        width: 170,
        formatter: actionFormatter
    };
    var fanyongs = [[{
            title: '一级名称',
            field: 'fxname1'
        },
        {
            title: '一级会费',
            field: 'huifei1'
        },
        {
            title: '一级折扣',
            field: 'mzhekou1'
        },
        {
            title: '一级费率',
            field: 'txfei1'
        }],
        [{
            title: '二级名称',
            field: 'fxname2'
        },
        {
            title: '二级会费',
            field: 'huifei2'
        },
        {
            title: '二级折扣',
            field: 'mzhekou2'
        },
        {
            title: '二级费率',
            field: 'txfei2'
        }],
        [{
            title: '三级名称',
            field: 'fxname3'
        },
        {
            title: '三级会费',
            field: 'huifei3'
        },{
                title: '三级折扣',
                field: 'mzhekou3'
            },
            {
                title: '三级费率',
                field: 'txfei3'
            }],
        [{
            title: '四级名称',
            field: 'fxname4'
        },
        {
            title: '四级会费',
            field: 'huifei4'
        },{
            title: '四级折扣',
            field: 'mzhekou4'
        },{
                title: '四级费率',
                field: 'txfei4'
            }],
        [{
            title: '五级名称',
            field: 'fxname5'
        },
        {
            title: '五级会费',
            field: 'huifei5'
        },{
            title: '五级折扣',
            field: 'mzhekou5'
        },{
            title: '五级费率',
            field: 'txfei5'
        }],
        [{
            title: '六级名称',
            field: 'fxname6'
        },
        {
            title: '六级会费',
            field: 'huifei6'
        },{
            title: '六级折扣',
            field: 'mzhekou6'
        },{
            title: '六级费率',
            field: 'txfei6'
        }],
        [{
            title: '七级名称',
            field: 'fxname7'
        },
        {
            title: '七级会费',
            field: 'huifei7'
        },{
            title: '七级折扣',
            field: 'mzhekou7'
        },{
            title: '七级费率',
            field: 'txfei7'
        }]];
    var columns = [
        {
            title: '全选',
            field: 'select',
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            title: 'ID',
            field: 'id',
            visible: false
        },
        // {
        //     title: '分销类型',
        //     field: 'fxtype',
        //     formatter: "fxtypeFormatter"
        // },
        {
            title: '分销级别',
            field: 'fxlevel'
        },
        {
            title: '见点奖励',
            field: 'jiandian'
        },
        {
            title: '升级会员方式',
            field: 'huiyuantype',
            formatter: huiyuantypeFormatter
        },
        // {
        //     title: '团队奖',
        //     field: 'tuanjiang'
        // },
    ];

    function initColumns() {
        for(var i=1;i<=7;i++){
            columns.push(fanyongs[i-1][0])
            columns.push(fanyongs[i-1][1])
            columns.push(fanyongs[i-1][2])
            columns.push(fanyongs[i-1][3])
        }
        columns.push(act)
        return columns;
    }

    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url: "/admin/fenxiao/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination: true,//是否分页
        queryParamsType: 'limit',
        queryParams: queryParams,
        sidePagination: 'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize: 50,//单页记录数
        pageList: [50, 100, 200, 500],//分页步进值
        showExport: true,//显示导出
        showRefresh: false,//刷新按钮
        showColumns: false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns: initColumns(),
        locale: 'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function queryParams(params) {
        var p = {
            limit: params.limit,
            offset: params.offset,
            sort: params.sort,
            order: params.order
            , search: $('#search').val() || ""
            , status: $('#qstatus').val() || 99
        }
        return p;
    }

    $('#search_btn').click(function () {
        var p = {
            status: $('#qstatus').val(),
            search: $('#search').val(),
            ts: new Date().getTime()
        };
        $('#mytable').bootstrapTable('refresh', p);
    })

    /*
     * 验证错误提示信息高亮
     */
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        }, errorElement: "span", errorPlacement: function (e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }), $("#aform").validate(), $("#mform").validate();
})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name") + "=" + $(this).val())
        }
    });

    return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id") + "=" + $(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id") + "=" + $(this).val())
        }
    });

    return true;
}